﻿#region Copyright Notice
// 
// 
// Copyright (c) 2009-2010 Anindya Chatterjee
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
// 
// 
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using DesktopNotifier.Properties;
using Event.Plugin;
using Event.Plugin.Utility;

namespace DesktopNotifier.Utility
{
    public class NotifyUtility : INotifier
    {
        private static bool _negative;
        private NotifyHost _host;

        static NotifyUtility()
        {
            IconPool = new List<Icon> {Resources.DesktopAlert, Resources.DesktopAlertInvert};
        }

        private static List<Icon> IconPool { get; set; }

        internal static bool IsAnimating { get; set; }
        internal static bool ShowBalloonTip { get; set; }

        #region INotifier Members

        public void StartAnimateIcon()
        {
            IsAnimating = true;
        }

        public void StopAnimateIcon()
        {
            IsAnimating = false;
        }

        /// <summary>
        /// Shows notify tip window with the necessary info
        /// </summary>
        /// <param name="tipText">Main text for the tip</param>
        /// <param name="tipHeading">Heading of the tip</param>
        /// <param name="tipIcon">Icon for the tip
        /// <param name="sender">Sender event</param>
        /// <remarks>If icon is null default icon will be the app icon</remarks>
        /// </param>
        public void ShowNotifyTipNow(string tipText, string tipHeading, Icon tipIcon, AbstractEvent sender)
        {
            var tip = new NotifyTip(sender)
                          {
                              Text = "File Size",
                              TipText = tipText,
                              TipHeading = tipHeading,
                              TipIcon = tipIcon ?? Resources.DesktopAlert
                          };
            ShowBalloonTip = true;
            if (_host != null && !_host.IsDisposed)
                _host.RegisterNotifyTip(tip);
            else
            {
                _host = new NotifyHost(this);
                _host.RegisterNotifyTip(tip);
            }
        }

        /// <summary>
        /// Show the custom notify tip window with necessary info
        /// </summary>
        /// <param name="notifyTip">Custom notify tip to show</param>
        public void ShowNotifyTipNow(BaseNotifyTip notifyTip)
        {
            ShowBalloonTip = true;
            if (_host != null && !_host.IsDisposed)
                _host.RegisterNotifyTip(notifyTip);
            else
            {
                _host = new NotifyHost(this);
                _host.RegisterNotifyTip(notifyTip);
            }
        }

        /// <summary>
        /// Plays a default sound
        /// </summary>
        public void PlaySound()
        {
            PlaySound(Resources.Notify);
        }

        /// <summary>
        /// Plays a notification sound from the file
        /// </summary>
        /// <param name="soundFile">Location of the sound file</param>
        /// <remarks>Pass null to play a default sound</remarks>
        public void PlaySound(string soundFile)
        {
            try
            {
                if (!string.IsNullOrEmpty(soundFile) && File.Exists(soundFile))
                    SoundUtility.PlaySound(soundFile);
                else
                    PlaySound();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error in Ozone");
            }
        }

        /// <summary>
        /// Plays a notfication sound from the stream
        /// </summary>
        /// <param name="sound">Sound stream</param>
        /// <remarks>Pass null values to play default sound</remarks>
        public void PlaySound(Stream sound)
        {
            try
            {
                if (sound != null)
                    SoundUtility.PlaySound(sound);
                else
                    PlaySound();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error in Ozone");
            }
        }

        #endregion

        internal static Icon GetAnimatedIcon()
        {
            if (_negative)
            {
                _negative = false;
                return IconPool[1];
            }
            _negative = true;
            return IconPool[0];
        }
    }
}